package com.company.project.controller.goods;

import com.company.project.entity.SysStoreEntity;
import com.company.project.service.HttpSessionService;
import com.company.project.service.SysStoreService;
import com.company.project.vo.resp.goods.TypeRespNodeVO;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysGoodsTypeEntity;
import com.company.project.service.SysGoodsTypeService;


/**
 * 商品类型表
 *
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-08-08 13:44:35
 */
@Controller
@RequestMapping("/")
@Api(tags = "商品管理-商品类型")
public class  SysGoodsTypeController {
    @Autowired
    private SysGoodsTypeService sysGoodsTypeService;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysGoodsType")
    public String sysGoodsType() {
        return "sysgoodstype/list";
    }

    @ApiOperation(value = "新增")
    @PostMapping("sysGoodsType/add")
    @RequiresPermissions("sysGoodsType:add")
    @ResponseBody
    public DataResult add(@RequestBody SysGoodsTypeEntity sysGoodsType) {
        sysGoodsTypeService.addGoodsType(sysGoodsType);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysGoodsType/delete")
    @RequiresPermissions("sysGoodsType:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysGoodsTypeService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysGoodsType/update")
    @RequiresPermissions("sysGoodsType:update")
    @ResponseBody
    public DataResult update(@RequestBody SysGoodsTypeEntity sysGoodsType) {
        sysGoodsTypeService.updateType(sysGoodsType);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysGoodsType/listByPage")
    @RequiresPermissions("sysGoodsType:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysGoodsTypeEntity sysGoodsType){
        Page page = new Page(sysGoodsType.getPage(), sysGoodsType.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
//        queryWrapper.eq("id", sysGoodsType.getId());
        if(StringUtils.isEmpty(sysGoodsType.getLevel())) {
            queryWrapper.eq("level", sysGoodsType.getLevel());
        }
        if(StringUtils.isEmpty(sysGoodsType.getPid())){
            queryWrapper.eq("pid", sysGoodsType.getLevel());
        }

        IPage<SysGoodsTypeEntity> iPage = sysGoodsTypeService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @GetMapping("sysGoodsType/list")
    @ApiOperation(value = "获取所有商品类型")
//    @LogAnnotation(title = "菜单权限管理", action = "获取所有菜单权限")
    @RequiresPermissions("sysGoodsType:list")
    @ResponseBody
    public DataResult<List<SysGoodsTypeEntity>> getAllGoodsType() {
        return DataResult.success(sysGoodsTypeService.selectAll());
    }

    @GetMapping("sysGoodsType/terr")
    @ApiOperation(value = "获取所有商品类型树")
//    @LogAnnotation(title = "菜单权限管理", action = "获取所有菜单权限")
    @RequiresPermissions("sysGoodsType:list")
    @ResponseBody
    public DataResult<List<TypeRespNodeVO>> getAllGoodsTypeTree(@RequestParam(required = false) String goodsTypeId) {
        return DataResult.success(sysGoodsTypeService.selectAllTree(goodsTypeId));
    }
}
